package com.GLM.Viewer.Map;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.GLM.Viewer.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.ItemizedOverlay.OnFocusChangeListener;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class MyItemizedOverlay extends ItemizedOverlay implements OnFocusChangeListener, OnClickListener, OnDoubleTapListener, OnGestureListener {

	private static final String TAG = "MyItemizedOverlay";
	private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private GestureDetector gestureScanner = new GestureDetector(this);

	private MapMainActivity mContext;
	private MapView mMapView;
	private MapController mMapCtrl;
	private View mPopView;
	private Handler mHandler;

	private GeoPoint point = null;
	private String desc = "";

	private Drawable mItemDrawable;
	private Drawable mItemSelectDrawable;
	private int mLayout_OffsetX = 0;
	private int mLayout_OffsetY = -30;
	private int level = 0;

	private OverlayItem selectItem;
	private OverlayItem lastItem;

	public MyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	public MyItemizedOverlay(Drawable defaultMarker, Context context, MapView mapView, View popView, MapController mapCtrl, Handler handler) {
		super(boundCenterBottom(defaultMarker));

		mItemDrawable = defaultMarker;
		mItemSelectDrawable = defaultMarker;
		mLayout_OffsetX = mItemDrawable.getBounds().centerX();
		mLayout_OffsetY = -mItemDrawable.getBounds().height();

		mContext = (MapMainActivity) context;
		setOnFocusChangeListener(this);
		mMapView = mapView;
		mPopView = popView;
		mMapCtrl = mapCtrl;
		mHandler = handler;
	}

	public void setItemSelectDrawable(Drawable itemSelectDrawable) {
		this.mItemSelectDrawable = itemSelectDrawable;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public void addOverlay(OverlayItem item) {
		mOverlays.add(item);
		populate();
	}

	public void removeOverlay(int location) {
		mOverlays.remove(location);
	}

	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		Log.d(TAG, "onTap 2 params");
		return super.onTap(p, mapView);
	}

	@Override
	protected boolean onTap(int index) {
		Log.d(TAG, "onTap index");
		return super.onTap(index);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
	}

	public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {
		Log.d(TAG, "item focus changed!");
		if (null != newFocus) {
			// Log.d(TAG, "centerY : " + mItemDrawable.getBounds().centerY() + "; centerX :" + mItemDrawable.getBounds().centerX());
			// Log.d(TAG, " height : " + mItemDrawable.getBounds().height());
			MapView.LayoutParams params = (MapView.LayoutParams) mPopView.getLayoutParams();
			params.x = this.mLayout_OffsetX;
			params.y = this.mLayout_OffsetY;
			point = newFocus.getPoint();
			params.point = point;
			mMapCtrl.animateTo(point);
			TextView title_TextView = (TextView) mPopView.findViewById(R.id.map_bubbleTitle);
			title_TextView.setText(newFocus.getTitle());
			TextView desc_TextView = (TextView) mPopView.findViewById(R.id.map_bubbleText);
			if (null == newFocus.getSnippet() || "".equals(newFocus.getSnippet())) {
				desc_TextView.setVisibility(View.GONE);
			} else {
				desc = newFocus.getSnippet();
				desc_TextView.setText(desc);
				desc_TextView.setVisibility(View.VISIBLE);
			}
			RelativeLayout button = (RelativeLayout) mPopView.findViewById(R.id.map_bubblebtn);
			button.setOnClickListener(this);
			mMapView.updateViewLayout(mPopView, params);
			mPopView.setVisibility(View.VISIBLE);
			selectItem = newFocus;
		}
	}

	public void onClick(View v) {
		if (v.getId() == R.id.map_bubblebtn) {
			mHandler.sendEmptyMessage(mContext.MSG_VIEW_BUBBLEPOPUP_CLICK);
		} else {

		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		return gestureScanner.onTouchEvent(event);
	}

	public boolean onDown(MotionEvent e) {
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}

	public void onLongPress(MotionEvent e) {
		Log.d(TAG, "onLongPress");
		mContext.setLocaPointByProjectionPixels((int) e.getX(), (int) e.getY());
		mHandler.sendEmptyMessage(mContext.MSG_VIEW_LONGPRESS);

	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	public void onShowPress(MotionEvent e) {

	}

	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	public boolean onDoubleTap(MotionEvent e) {
		Log.d(TAG, "onDoubleTap");
		mMapCtrl.zoomIn();
		return false;
	}

	public boolean onDoubleTapEvent(MotionEvent e) {
		return false;
	}

	public boolean onSingleTapConfirmed(MotionEvent e) {
		return false;
	}

}